
#include "reduce_log_sum_exp_tiling.h"
#include "register/op_def_registry.h"


namespace optiling {
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{

  ReduceLogSumExpTilingData tiling;
  const gert::StorageShape* x1_shape = context->GetInputShape(0);
  const bool *attr0 = context->GetAttrs()->GetAttrPointer<bool>(0);

  int32_t data_sz = 1;
  uint64_t zipN = 0;
// 0~9 10~19 20~39 40~59 60~60       61~63
// N0   N1    N2    N3    keep_dim   dim_num
  for (int i = 0; i < x1_shape->GetStorageShape().GetDimNum(); i++)
    data_sz *= x1_shape->GetStorageShape().GetDim(i);
  if(0 < x1_shape->GetStorageShape().GetDimNum()) {
    zipN |= x1_shape->GetStorageShape().GetDim(0);
  }
  if(1 < x1_shape->GetStorageShape().GetDimNum()) {
    zipN |= x1_shape->GetStorageShape().GetDim(1) << 10;
  }
  if(2 < x1_shape->GetStorageShape().GetDimNum()) {
    zipN |= (uint64_t)x1_shape->GetStorageShape().GetDim(2) << 20;
  }
  if(3 < x1_shape->GetStorageShape().GetDimNum()) {
    zipN |= (uint64_t)x1_shape->GetStorageShape().GetDim(3) << 40;
  }
  if(*attr0) {
    zipN |= 1ll << 60;
  }
  zipN |= (uint64_t) x1_shape->GetStorageShape().GetDimNum() << 60;
  tiling.set_zipN(zipN);
  context->SetBlockDim(8);
  tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
  context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());

  return ge::GRAPH_SUCCESS;
}
}


namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
static ge::graphStatus InferDataType(gert::InferDataTypeContext *context)
{
const auto inputDataType = context->GetInputDataType(0);
context->SetOutputDataType(0, inputDataType);
return ge::GRAPH_SUCCESS;
}
}


namespace ops {
class ReduceLogSumExp : public OpDef {
public:
    explicit ReduceLogSumExp(const char* name) : OpDef(name)
    {
        this->Input("x")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("axes")
            .ParamType(REQUIRED)
            .DataType({ge::DT_INT32, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("y")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Attr("keep_dims").Bool();

        this->SetInferShape(ge::InferShape).SetInferDataType(ge::InferDataType);

        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend910");

    }
};

OP_ADD(ReduceLogSumExp);
}
